package common.conf.Service.helper;

import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.ServiceConnection;
import android.os.Handler;
import android.os.IBinder;
import android.os.Message;
import android.widget.TextView;

import com.amap.api.location.AMapLocation;
import com.amap.api.services.core.LatLonPoint;
import com.orhanobut.logger.Logger;
import com.tekj.cxqcb.config.enums.EventBusMsgBean;

import org.greenrobot.eventbus.EventBus;


import common.conf.Service.LocationService;

import static android.content.Context.BIND_AUTO_CREATE;
import static com.tekj.cxqcb.config.enums.EventBusMsgBean.EnumStatus.定位_区;
import static com.tekj.cxqcb.config.enums.EventBusMsgBean.EnumStatus.定位_市;
import static com.tekj.cxqcb.config.enums.EventBusMsgBean.EnumStatus.定位_省;
import static com.tekj.cxqcb.config.enums.EventBusMsgBean.EnumStatus.定位_经纬度;
import static com.tekj.cxqcb.config.enums.EventBusMsgBean.EnumStatus.定位_详细位置;
import static com.tekj.cxqcb.config.enums.EventBusMsgBean.EnumStatus.定位_道路;

/**
 * 城市名称设置，方法调用辅助服务类
 * Created by Tongzhenggang@gmail.com on 2018/5/4.
 */
public class CityNameSet implements ServiceConnection {
    private Context mContext;
    private TextView mTextView;
    private int type;

    public CityNameSet(Context context, TextView textview, int type) {
        this.mContext = context;
        this.mTextView = textview;
        this.type = type;
        //开启定位服务
        context.bindService(new Intent(context, LocationService.class), this, BIND_AUTO_CREATE);
    }


    @Override
    public void onServiceConnected(ComponentName name, IBinder service) {
        Logger.i("onServiceConnected");

        LocationService.Binder binder = (LocationService.Binder) service;
        LocationService mLocationService = binder.getService();
        mLocationService.setCallBack(new LocationService.CallBack() {
            @Override
            public void onDataChange(AMapLocation aMapLocation) {
                Message msg = new Message();
                msg.obj = aMapLocation;
                mHandler.sendMessage(msg);
            }

            @Override
            public void onError(String errorInfo) {
//                Logger.e("onError>>>"+errorInfo);
                mContext.unbindService(CityNameSet.this);
            }
        });

    }

    @Override
    public void onServiceDisconnected(ComponentName name) {
        Logger.i("onServiceDisconnected");
        mContext.unbindService(this);
        //开启定位服务
        mContext.bindService(new Intent(mContext, LocationService.class), this, BIND_AUTO_CREATE);
    }

    private Handler mHandler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            AMapLocation aMapLocation = (AMapLocation) msg.obj;
            String city = aMapLocation.getCity();
            Logger.i("onDataChange>>>city=" + city);
//            Toasty.normal(getActivity(), "city=" + city).show();
            /**
             * MyEvent  id = 1 当获取到当前坐标后刷新去抢单列表
             */
            EventBus.getDefault().post(new EventBusMsgBean(定位_省, aMapLocation.getProvince()));
            EventBus.getDefault().post(new EventBusMsgBean(定位_市, aMapLocation.getCity()));
            EventBus.getDefault().post(new EventBusMsgBean(定位_区, aMapLocation.getDistrict()));
            EventBus.getDefault().post(new EventBusMsgBean(定位_道路, aMapLocation.getStreet() + aMapLocation.getStreetNum()));
            EventBus.getDefault().post(new EventBusMsgBean(定位_详细位置, aMapLocation.getPoiName()));
            EventBus.getDefault().post(new EventBusMsgBean(定位_经纬度,
                    new LatLonPoint(aMapLocation.getLatitude(), aMapLocation.getLongitude())));

            if (type == 1) {
                mTextView.setText(city);
            } else if (type == 2) {
                mTextView.setText(aMapLocation.getAddress());
            }


            mContext.unbindService(CityNameSet.this);
        }
    };
}
